package com.zhu.rpc.client;

import com.zhu.rpc.client.handler.ClientHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.concurrent.Future;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RpcClient {
    Logger logger = LoggerFactory.getLogger(RpcClient.class);
    //加上注解暴露某个接口并且使用代理执行该接口，然后返回远程过程调用的值
    public ChannelFuture connect(int port, String host,String serverName) throws InterruptedException {
        EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
        Bootstrap b = new Bootstrap();
        b.group(eventLoopGroup)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, true)
//                .option(ChannelOption.SO_KEEPALIVE, true)
                .handler(new RpcClientChannelInitializer(serverName,port,host));
        return b.connect(host,port).sync();
//        return b.connect(host,port).sync().addListener(new ConnectionListener(b,host,port));
//        return b.connect(host,port).addListener((future) -> {
//            if (future.isSuccess()){
//                logger.info("Rpc客户端已启动 连接端口为{}",port);
//            }else {
//                logger.info("Rpc客户端启动失败 连接端口为{}！！！",port);
//            }
//        });
    }


}
